﻿//项目基础js

; !(function (over, win, slf) {
    win.apj = {
        getCookie: function (cname) {
            var name = cname + "=";
            var decodedCookie = decodeURIComponent(document.cookie);
            var ca = decodedCookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) === ' ') {
                    c = c.substring(1);
                }
                if (c.indexOf(name) === 0) {
                    return c.substring(name.length, c.length);
                }
            }
            return "";
        },
        getLayxId: function () {
            var id;
            if (self !== top && self.frameElement && self.frameElement.tagName === "IFRAME") {
                var layxWindow = $(self.frameElement).parents(".layx-window");
                id = layxWindow.attr("id").substr(5);
            }
            return id;
        },
        confirm: function (msg, success) {
            top.layx.confirm('系统提示', msg, success, {
                id: 'apj-confirm',
                dialogIcon: 'warn'
            });
        },
        success: function (msg, yes, destroy) {
            top.layx.alert('系统提示', msg, yes, {
                id: 'apj-alert-success',
                dialogIcon: 'success',
                event: {
                    ondestroy: { before: destroy }
                }
            });
        },
        successWithButtons: function (msg, yes, buttons, destroy) {
            top.layx.alert('系统提示', msg, yes, {
                id: 'apj-alert-success',
                dialogIcon: 'success',
                buttons: buttons,
                event: {
                    ondestroy: { before: destroy }
                }
            });
        },
        warn: function (msg, yes, destroy) {
            top.layx.alert('系统提示', msg, yes, {
                id: 'apj-alert-warn',
                dialogIcon: 'warn',
                event: {
                    ondestroy: { before: destroy }
                }
            });
        },
        error: function (msg, yes, destroy) {
            top.layx.alert('系统提示', msg, yes, {
                id: 'apj-alert-error',
                dialogIcon: 'error',
                event: {
                    ondestroy: { before: destroy }
                }
            });
        },
        load: function (msg) {
            top.layx.load('apj-load', msg);
        },
        closeLoad: function () {
            top.layx.destroy('apj-load');
        },
        get: function (url, data, success) {
            var that = this;
            $.ajax({
                url: url,
                data: data || {},
                type: 'get',
                dataType: 'json',
                beforeSend: function (xhr) {
                    that.load("正在读取数据");
                },
                success: function (data, textStatus) {
                    if (typeof success === "function") {
                        success(data, textStatus);
                    }
                },
                error: function (xhr, status, error) {
                    that.error("程序异常，请联系服务商。");
                },
                complete: function (xhr, status) {
                    that.closeLoad();
                }
            });
        },
        post: function (url, data, success) {
            var that = this;
            $.ajax({
                url: url,
                data: data || {},
                type: 'post',
                headers: {
                    "X-CSRF-TOKEN": that.getCookie("XSRF-TOKEN")
                },
                dataType: 'json',
                beforeSend: function (xhr) {
                    that.load("正在提交数据");
                },
                success: function (data, textStatus) {
                    if (typeof success === "function") {
                        success(data, textStatus);
                    }
                },
                error: function (xhr, status, error) {
                    that.error("程序异常，请联系服务商。");
                },
                complete: function (xhr, status) {
                    that.closeLoad();
                }
            });
        }
    };
})(top, window.self);
